/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.app.converter;

import com.alibaba.fastjson.JSON;
import com.itgrail.pilot.app.command.cmo.AddObjectiveCmd;
import com.itgrail.pilot.app.command.cmo.DeleteObjectiveCmd;
import com.itgrail.pilot.app.command.cmo.UpdateObjectiveCmd;
import com.itgrail.pilot.app.command.co.ObjectiveDetailCO;
import com.itgrail.pilot.domain.user.entity.ObjectiveE;
import com.itgrail.pilot.domain.user.factory.ObjectiveFactory;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.ObjectiveDO;
import com.itgrail.pilot.infrastructure.tunnel.util.SessionUtils;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.itgrail.grail.convertor.ConverterI;
import com.itgrail.grail.utils.reflect.BeanMapper;

@Component
public class ObjectiveClientConverter implements ConverterI {

    @Autowired
    private ObjectiveFactory factory;

    public ObjectiveE addClientToEntity(AddObjectiveCmd cmd) {
        ObjectiveE objectiveE = factory.createObjectiveE();
        BeanUtils.copyProperties(cmd, objectiveE);
        if (SessionUtils.currentUserId() != null) {
            objectiveE.setCreated(SessionUtils.currentUserId());
        }
        return objectiveE;
    }

    public ObjectiveE updateClientToEntity(UpdateObjectiveCmd cmd) {
        ObjectiveE objectiveE = factory.createObjectiveE();
        BeanUtils.copyProperties(cmd, objectiveE);
        if (SessionUtils.currentUserId() != null) {
            objectiveE.setUpdated(SessionUtils.currentUserId());
        }
        if (StringUtils.isNotEmpty(cmd.getUserComment())) {
            objectiveE.setMyEvaluate(cmd.getUserComment());
        }
        if (StringUtils.isNotEmpty(cmd.getLeaderComment())) {
            objectiveE.setLeaderEvaluate(cmd.getLeaderComment());
        }
        objectiveE.setMyEvaluate(cmd.getUserComment());
        objectiveE.setLeaderEvaluate(cmd.getLeaderComment());
        objectiveE.setMyScore(cmd.getUserScore());
        objectiveE.setLeaderScore(cmd.getLeaderScore());
        objectiveE.setNotifyUserList(JSON.toJSONString(cmd.getNotifyUsers()));
        return objectiveE;
    }


    public ObjectiveE deleteClientToEntity(DeleteObjectiveCmd cmd) {
        ObjectiveE objectiveE = factory.createObjectiveE();
        BeanUtils.copyProperties(cmd, objectiveE);
        objectiveE.setDeleted(new Byte("1"));
        if (SessionUtils.currentUserId() != null) {
            objectiveE.setUpdated(SessionUtils.currentUserId());
        }
        return objectiveE;
    }

    public ObjectiveDetailCO dataToClient(ObjectiveDO objectiveDO) {
        ObjectiveDetailCO map = BeanMapper.map(objectiveDO, ObjectiveDetailCO.class);
        if (StringUtils.isNotEmpty(objectiveDO.getMyEvaluate())) {
            map.setUserComment(objectiveDO.getMyEvaluate());
        }
        if (StringUtils.isNotEmpty(objectiveDO.getLeaderEvaluate())) {
            map.setLeaderComment(objectiveDO.getLeaderEvaluate());
        }
        return map;
    }

  /*   public List<OkrPeriodDetailCO>  dataToClients(List<OkrPeriodDO> records) {
        return Optional.ofNullable(records).orElse(Arrays.asList())
                .stream()
                .map(this::dataToClient)
                .collect(Collectors.toList());
    }*/
}
